#include <stdio.h>

#include "data_struct.h"

#define NAMESIZE	32

static void show_stu(const void *data);
static int cmp_id(const void *data, const void *key);

typedef struct stu_st {
	int id;
	char name[NAMESIZE];
}stu_t;

int main(void)
{
	seqlist_t *me = NULL;
	int i;
	stu_t s, *f;
	int find_id;
	int new;

	// 创建表
	seqlist_init(&me, sizeof(stu_t), 5);

	for (i = 0; i < 8; i++) {
		s.id = i + 1;
		snprintf(s.name, NAMESIZE, "winner%d", i + 1);
		if (-1 == seqlist_insert(me, &s)) {
			printf("插入失败\n");
			break;
		}
	}

	seqlist_traval(me, show_stu);

	printf("***********查找***************\n");
	find_id = 3;
	f = (stu_t *)seqlist_search(me, &find_id, cmp_id);
	if (NULL == f) {
		printf("没有查找元素\n");
	} else {
		printf("**************找到了！删除***************\n");
		seqlist_delete(me, &find_id, cmp_id);
		seqlist_traval(me, show_stu);
	}
	printf("***********查找***************\n");
	find_id = 5;
	f = (stu_t *)seqlist_search(me, &find_id, cmp_id);
	if (NULL == f) {
		printf("没有查找元素\n");
	} else {
		printf("**************找到了！更新***************\n");
		stu_t s1 = {999,"坤坤不打蓝桥"};
		seqlist_update(me, &find_id, cmp_id, &s1);
		seqlist_traval(me, show_stu);
	}


	seqlist_destroy(&me);

	return 0;
}

static void show_stu(const void *data)
{
	const stu_t *d = (const stu_t *)data;
	printf("%d %s\n", d->id, d->name);
}

static int cmp_id(const void *data, const void *key)
{
	const stu_t *d = (const stu_t *)data;
	const int *k = (const int *)key;

	return d->id - *k;
}





